<?php

namespace App\Http\Controllers\api;

use App\Admin\Repositories\ArticleComment;
use App\Http\Controllers\Controller;
use App\Models\AdminComment;
use App\Models\AdminArticle;
use App\Models\AdminPrefecture;
use App\Services\RedisFun;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Predis\Client;

class AdminLearnCommunity extends Controller
{
    public function __construct()
    {
        $this->predis = new RedisFun('127.0.0.1:6379');
    }

    /**
     * @return array
     * 社区类型
     */
    public function communityType(){
        $communityType = AdminPrefecture::where('status',1)->get();
        return ['code'=>1010,'msg'=>'学习社区信息','data'=>$communityType];
    }
    /**
     * 获取文章
     */
    public function getArticleList(Request $request){
        $articleId = $request->get('articleId',1);
        if(!is_numeric($articleId)){
            return ['code'=>99999,'msg'=>'数据参数不正确','data'=>''];
        }
        try {
            $articleInfo = AdminArticle::with('userInfo')->where('academy_id',$articleId)->get()->toArray();
            if(empty($articleInfo)){
                return ['code'=>1012,'msg'=>'暂无信息,请换一个试试','data'=>''];
            }
            foreach ($articleInfo as $k => $v){
                $articleInfo[$k]['content'] = mb_substr($v['content'],0,35)."...";
                $articleInfo[$k]['user_info']['phone'] = substr($v['user_info']['phone'],0,3)."****".substr($v['user_info']['phone'],7,4);
            }
            return ['code'=>1011,'msg'=>'文章信息','data'=>$articleInfo];
        }catch (\Exception $e){
            return ['code'=>1013,'msg'=>'加载失败'.$e->getMessage(),'data'=>''];
        }
    }
    /**
     * @return array
     * 热门文章
     */
    public function hotArticle(){
        $hotArticle = $this->predis->zrevrange('hostArticle',0,-1);
        if(empty($hotArticle)){
            $articelInfo = AdminArticle::orderBy('page_view','desc')->limit(6)->get()->toArray();
            foreach ($articelInfo as $v){
                $this->predis->zadd('hostArticle',$v['page_view'],json_encode($v));
            }
            return ['code'=>1020,'msg'=>'更新文章信息','data'=>$articelInfo];
        }else{
            foreach ($hotArticle as $k => $v){
                $hotArticle[$k] = json_decode($v);
            }
            return ['code'=>1020,'msg'=>'获取热门文章信息','data'=>$hotArticle];
        }
    }
    /**
     * 文章详情
     */
    public function articleParticulars(Request $request){
        $articleId = $request->get('articleId');
        if(!is_numeric($articleId)){
            return ['code'=>99999,'msg'=>'数据参数不正确','data'=>''];
        }
        try{
            $articleParticulars = AdminArticle::with('userInfo')->where('id',$articleId)->get();
            //pv浏览量加一
            AdminArticle::where('id',$articleId)->update(['page_view'=>$articleParticulars[0]['page_view']+1]);
            return ['code'=>1030,'msg'=>'获取文章详情','data'=>$articleParticulars];
        }catch (\Exception $e){
            return ['code'=>1031,'msg'=>'获取详情失败'.$e->getMessage(),'data'=>''];
        }
    }
    /**
     * 文章评论
     */
    public function articleComment(Request $request){
        $articleId = $request->post('articleId');
        if(!is_numeric($articleId)){
            return ['code'=>99999,'msg'=>'数据参数不正确','data'=>''];
        }
        $getArticleComment = AdminComment::with('userInfo')->where('article_id',$articleId)->get();
        $articleComment = (new AdminComment())->comment($getArticleComment);
        return ['code'=>1035,'msg'=>'文章评论信息','data'=>$articleComment,];
    }
}
